package com.woniuxy.propertyjava.controller;


import com.woniuxy.propertyjava.entity.PageBean;
import com.woniuxy.propertyjava.entity.Patrolplan;
import com.woniuxy.propertyjava.entity.ResponseResult;
import com.woniuxy.propertyjava.service.PatrolplanService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Console;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping
public class PatrolplanController {
    @Resource
    private PatrolplanService patrolplanService;

    //分页查询
@PostMapping("/patrolplan/{pageIndex}/{pageSize}")
    ResponseResult<PageBean<Patrolplan>> queryByPage(@RequestBody Patrolplan patrolplan,
                                                     @PathVariable int pageIndex,
                                                     @PathVariable("pageSize") int pageSize) throws ParseException {
    System.out.println(patrolplan);


    PageBean<Patrolplan> pageBean = new PageBean<>();



    pageBean=patrolplanService.queryByPage(patrolplan,pageIndex,pageSize);
    ResponseResult<PageBean<Patrolplan>> responseResult = new ResponseResult<>();
    responseResult.setCode(200);
    responseResult.setMsg("查询成功");
    responseResult.setData(pageBean);
    return responseResult;
}
    //单个查询
    @GetMapping("/patrolplan")
    public ResponseResult<PageBean<Patrolplan>> queryById( int id){
        //System.out.println(id);
        PageBean<Patrolplan> pageBean = new PageBean<>();


        Patrolplan patrolplan = patrolplanService.queryById(id);
        List<Patrolplan> list=new ArrayList();
        list.add(patrolplan);
        pageBean.setBeanList(list);
        pageBean.setPageIndex(1);
        pageBean.setPageSize(1);
        pageBean.setTotalPage(1);

        ResponseResult<PageBean<Patrolplan>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setMsg("查询成功");
        responseResult.setData(pageBean);
        return responseResult;
    }
//新增数据
    @PostMapping("/patrolplan")
    public ResponseResult add(@RequestBody Patrolplan patrolplan){
        //System.out.println(patrolplan);
    patrolplanService.insert(patrolplan);
        Patrolplan patrolplan1 = patrolplanService.queryById(patrolplan.getId());
        ResponseResult<Patrolplan> responseResult = new ResponseResult<>(200, "ok", patrolplan1);


        return responseResult;
    }
    //修改数据
    @PutMapping("/patrolplan")
    public ResponseResult<Patrolplan> update(@RequestBody Patrolplan patrolplan){
        //System.out.println(patrolplan);
        Patrolplan update = patrolplanService.update(patrolplan);
        Patrolplan query = patrolplanService.queryById(patrolplan.getId());
        ResponseResult<Patrolplan> responseResult = new ResponseResult<>(200, "ok", query);
        return responseResult;


    }
    //删除数据
    @DeleteMapping("/patrolplan/{id}")
    public ResponseResult deleteById(@PathVariable int id){
    patrolplanService.deleteById(id);
        ResponseResult<String> responseResult = new ResponseResult<>(200, "ok", "");
        return responseResult;
    }

}
